import java.util.Arrays;

public class Solution1012 {
    public int numDupDigitsAtMostN(int n) {
        int ans=0,t=1;
        long now=10;
        while (now-1<=n){
            ans+=9*A(9,t-1);
            if (now-1==n){
                return n-ans;
            }
            now*=10;
            t++;
        }
        String s=String.valueOf(n);
        int[] f=new int[10];
        for (int i=0;i<s.length();i++){
            int can=s.charAt(i)-'0';
            for (int j=0;j<s.charAt(i)-'0';j++){
                if (i==0 && j==0 || f[j]==1){
                    can--;
                }
            }
            ans+=can*A(10-i-1,t-i-1);
            if (f[s.charAt(i)-'0']==1){
                break;
            }
            f[s.charAt(i)-'0']=1;
        }
        char[] chars=s.toCharArray();
        Arrays.sort(chars);
        boolean flag=true;
        for (int i=0;i<chars.length-1;i++){
            if (chars[i]==chars[i+1]){
                flag=false;
                break;
            }
        }
        if (flag){
            ans++;
        }
        return n-ans;
    }

    private int A(int n, int m) {
        int ans=1;
        while (m>0){
            ans=ans*n;
            n--;
            m--;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1012().numDupDigitsAtMostN(111));
    }
}
